// pages/deviceLocationRecord/deviceLocationRecord.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    items: [],
    newSeri: '',
    newPhone: '',
    newName: '',
    page: 1,
    pageSize: 10,
    isLoading: false,
    noMoreData: false,
    isRefreshing: false,
    showDialog: false,
    scrollyHeight: wx.getSystemInfoSync().windowHeight,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id)
    let scrollyHeight = getApp().px2rpx(wx.getSystemInfoSync().windowHeight) - 110
    this.setData({
      scrollyHeight,
    })
    this.setData({
      id: options.id
    })
    this.deviceLocationRecord()
  },


  /**
   * 获取设备坐标信息
   */
  deviceLocationRecord: function (refresh = false) {
    var that = this;
    if (this.data.isLoading) return;
    this.setData({
      isLoading: true
    });
    const {
      page,
      pageSize,
    } = this.data;
    wx.showLoading({
      title: '加载中',
    })
    let params = {
      page: page,
      limit: pageSize,
      deviceId: this.data.id
    }
    getApp().api.deviceLocationRecord(params, {
      success: function (res) {
        if (res.data.rows) {
          let items = res.data.rows
          items = refresh ? items : that.data.items.concat(items),
            items.forEach(e => {
              e.fomatTime = getApp().util.formatTime5(e.modifyTimeValue)
            })
          that.setData({
            items: items,
            noMoreData: res.data.total > items.length ? false : true,
            isRefreshing: false,
            isLoading: false
          })
          wx.hideLoading()
        }
      },
      fail: function (err) {
        that.setData({
          isLoading: false
        });
        wx.hideLoading()
        wx.showToast({
          title: err.message,
          icon: 'none',
          success: function () {
            if (refresh) {
              that.setData({
                items: null,
              })
            }
          }
        })

      }
    });
  },

  loadMore() {
    if (this.data.noMoreData || this.data.isRefreshing) return;
    this.setData({
      page: this.data.page + 1
    })
    this.deviceLocationRecord()
  },

  refresh() {
    this.setData({
      isRefreshing: true,
      noMoreData: false,
      page: 1
    });
    this.deviceLocationRecord(true)
  },



  refreshLocation: function () {
    this.deviceLocationSync()
  },

  /**
   * 获取设备坐标信息
   */
  deviceLocationSync: function () {
    var that = this;
    wx.showLoading({
      title: '正在刷新',
    })
    let params = {
      deviceId: this.data.id
    }
    getApp().api.deviceLocationSync(params, {
      success: function (res) {
        wx.hideLoading()
        that.setData({
          page: 1
        });
        that.deviceLocationRecord(true)
      },
      fail: function (err) {
        wx.hideLoading()
        wx.showToast({
          title: err.message,
          icon: 'none',
        })
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})